package com.yiyue.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiyue.graduate.dao.FileDataMapper;
import com.yiyue.graduate.pojo.FileData;
import com.yiyue.graduate.service.FileDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * @author yiyuexiaofusheng
 * @date 2021/5/7
 */
@Service
public class FileDataServiceImpl implements FileDataService {

    @Autowired
    FileDataMapper fileDataMapper;


    @Override
    public FileData getFileData(Long courseId) {
        return fileDataMapper.getFileData(courseId);

    }

    @Override
    public FileData getFileById(Long courseId) {
        QueryWrapper<FileData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        return fileDataMapper.selectOne(queryWrapper);
    }

    @Override
    public FileData getFileById1(Long Id) {
        QueryWrapper<FileData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",Id);
        return fileDataMapper.selectOne(queryWrapper);
    }

    @Override
    public InputStream getFileInputStream(FileData file) {
        File files=new File(file.getFilePath());
        try {
            return new FileInputStream(files);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<FileData> queryAllFileData() {
        return fileDataMapper.queryAllFileData();
    }
}
